package com.example.back.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import javax.persistence.EntityManager;
import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Set;
public class JpaRepositoryReBuild<T, ID> extends SimpleJpaRepository<T, ID> {

  private final JpaEntityInformation<T, ?> entityInformation;
  private final EntityManager em;

  @Autowired
  public JpaRepositoryReBuild(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
      super(entityInformation, entityManager);
      this.entityInformation = entityInformation;
      this.em = entityManager;
  }

  /**
   * 通用save方法 ：新增/选择性更新
   */
  @Override
  @Transactional
  public <S extends T> S save(S entity) {
      
    ID entityId = (ID) this.entityInformation.getId(entity);
		if (entityInformation.isNew(entity)) {
			em.persist(entity);
			return entity;
		} else {
      T managedEntity = this.findById(entityId).orElse(null);
      if(managedEntity != null){
        
      }
      System.out.println("aaaaaaaaaaaaaa");
      return em.merge(entity);
		}
  }

}